package com.cherrish.demo.thread;

import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author cherrish
 * @time 2019-02-27 14:08
 * @name TestThread2
 * @desc:
 */
public class TestThread2 {
    public static void main(String[] args) {
        java.util.concurrent.ThreadPoolExecutor threadPoolExecutor =
                new ThreadPoolExecutor(10, //corePoolSize 核心线程数
                        100, //maximumPoolSize 最大线程数
                        30, //keepAliveTime 线程池中超过corePoolSize数目的空闲线程最大存活时间；
                        // TimeUnit keepAliveTime时间单位
                        TimeUnit.SECONDS,
                        //workQueue 阻塞任务队列
                        new LinkedBlockingQueue<Runnable>(1000),
//threadFactory 新建线程的工厂
                        Executors.defaultThreadFactory(),
//RejectedExecutionHandler当提交任务数超过maxmumPoolSize+workQueue之和时，
// 任务会交给RejectedExecutionHandler来处理
                        new ThreadPoolExecutor.AbortPolicy()
                );
        for (int i = 0; i < 100; i++) {
            threadPoolExecutor.submit(new Runnable() {
                @Override
                public void run() {
                    System.out.println(Thread.currentThread().getName() + ": Hello");
                }
            });
        }
/**
  * 如果不再需要新任务，请适当关闭threadPoolExecutor并拒绝新任务
  */
        threadPoolExecutor.shutdown();
    }
}
